package com.ruibang.glass.equipment.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.ruibang.glass.equipment.constant.Constants;
import com.ruibang.glass.equipment.domain.req.device.AreaManagePageReq;
import com.ruibang.glass.equipment.entity.device.AreaManage;
import com.ruibang.glass.equipment.service.AreaManageService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/equipment/area/manage")
public class AreaManageController {
    @Autowired
    private AreaManageService areaManageService;

    @PostMapping("list")
    @ApiOperation(value = "区域管理", response = ResultBody.class)
    public ResultBody getBoilerList(@RequestBody AreaManagePageReq areaManagePageReq) {
        return ResultBody.success(areaManageService.queryAreasList(areaManagePageReq));
    }

    @PostMapping("addChild")
    @ApiOperation(value = "添加子层级", response = ResultBody.class)
    public ResultBody addChild(@RequestBody AreaManage areaManage) {
        return ResultBody.success(areaManageService.addChild(areaManage));
    }

    @PostMapping("editAll")
    @ApiOperation(value = "添加子层级", response = ResultBody.class)
    public ResultBody editAll(@RequestBody AreaManage areaManage) {
        return ResultBody.success(areaManageService.updateById(areaManage));
    }

    @GetMapping("deleteAll/{id}")
    @ApiOperation(value = "添加子层级", response = ResultBody.class)
    public ResultBody deleteAll(@PathVariable("id") String id) {
        List<AreaManage> list = areaManageService.list(Wrappers.<AreaManage>lambdaQuery().eq(AreaManage::getParentId, id));
        if (CollectionUtils.isNotEmpty(list)) {
            return ResultBody.fail(300, "当前区域有子层级无法删除！");
        }
        areaManageService.lambdaUpdate().set(AreaManage::getIsDelete, Constants.ONE_STR).eq(AreaManage::getId, id).update();
        return ResultBody.success();
    }

    @GetMapping("getAreaNameList")
    @ApiOperation(value = "获取区域名称", response = ResultBody.class)
    public ResultBody getAreaNameList() {
        return ResultBody.success(areaManageService.getAreaNameList());
    }


}
